// Fill out your copyright notice in the Description page of Project Settings.


#include "Component/Dash.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"

// Sets default values for this component's properties
UDash::UDash()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = false;

	// ...
}


// Called when the game starts
void UDash::BeginPlay()
{
	Super::BeginPlay();
	
}

void UDash::ActivateAbility()
{
	Character = Cast<ACharacter>(GetOwner());
	check(Character);
	IsDashActive = true;
	FVector Temp = Character->GetActorForwardVector();
	Temp.Normalize();
	Character->LaunchCharacter(Temp * ForceMultiplier, true, true);
	FTimerHandle TempTimerHandle;
	GetWorld()->GetTimerManager().SetTimer(TempTimerHandle, this, &UDash::StopMovementImmediately, DashDuration * UGameplayStatics::GetGlobalTimeDilation(this), false);
}

void UDash::StopMovementImmediately() {
	check(Character);
	Character->GetCharacterMovement()->StopMovementImmediately();
	FTimerHandle UniqueHandle;
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &UDash::SetIsDashActive, false);
	GetWorld()->GetTimerManager().SetTimer(UniqueHandle, RespawnDelegate, 0.5f, false);
}

void UDash::SetIsDashActive(bool NewValue) {
	IsDashActive = NewValue;
}

